/*
 * Atencion.java
 *
 * Created on 5 de septiembre de 2008, 13:35
 */
package podologia;

import java.awt.Image;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.swing.ImageIcon;
import podologia.beansDB.Paciente;
import podologia.beansDB.Persona;
import podologia.beansDB.Turno;

/**
 *
 * @author  Hernan
 */
public class Atencion extends javax.swing.JInternalFrame {

    private Paciente pacienteselec = new Paciente();
    //private List<Turno> turnoList01 = new ArrayList<Turno>(); No usada
    private Calendar dia = Calendar.getInstance();
    private Boolean nuevoFlag;
    private podologia.beansDB.Atencion atencion = null;
    private Turno turnoSelected;

    /** Creates new form Atencion */
    public Atencion() {
        initComponents();
        turnoList.clear();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor...
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(podologia.PodologiaApp.class).getContext().getResourceMap(Atencion.class);
        PodologiaPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory(resourceMap.getString("PodologiaPUEntityManager.persistenceUnit")).createEntityManager(); // NOI18N
        personaQuery = java.beans.Beans.isDesignTime() ? null : PodologiaPUEntityManager.createQuery("SELECT p FROM Persona p");
        personaList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : personaQuery == null ? java.util.Collections.emptyList() : personaQuery.getResultList();
        pacienteQuery = java.beans.Beans.isDesignTime() ? null : PodologiaPUEntityManager.createQuery("SELECT p FROM Paciente p");
        pacienteList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : pacienteQuery == null ? java.util.Collections.emptyList() : pacienteQuery.setMaxResults(20).getResultList();
        atencionQuery = java.beans.Beans.isDesignTime() ? null : PodologiaPUEntityManager.createQuery("SELECT a FROM Atencion a");
        atencionList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : atencionQuery.getResultList();
        turnoQuery = java.beans.Beans.isDesignTime() ? null : PodologiaPUEntityManager.createQuery("SELECT t FROM Turno t");
        turnoList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : turnoQuery.getResultList();
        jPanelBuscar = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        buscar = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        contraer = new javax.swing.JButton();
        jTextField2 = new javax.swing.JTextField();
        jPanelAtencion = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        jCheckBox3 = new javax.swing.JCheckBox();
        jPanel3 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTTurnos = new javax.swing.JTable();
        jLabel12 = new javax.swing.JLabel();
        jPanelBotonera = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jToggleButton1 = new javax.swing.JToggleButton();
        jPanelHc = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();

        setBorder(javax.swing.BorderFactory.createEtchedBorder());
        setClosable(true);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setAutoscrolls(true);
        setName("Form"); // NOI18N

        jPanelBuscar.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanelBuscar.setName("jPanelBuscar"); // NOI18N
        jPanelBuscar.setPreferredSize(new java.awt.Dimension(792, 65));

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        buscar.setFont(resourceMap.getFont("buscar.font")); // NOI18N
        buscar.setText(resourceMap.getString("buscar.text")); // NOI18N
        buscar.setName("buscar"); // NOI18N
        buscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarActionPerformed(evt);
            }
        });

        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField1KeyReleased(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setName("jTable1"); // NOI18N

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, pacienteList, jTable1);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${idPersona.apellido}"));
        columnBinding.setColumnName("Id Persona.apellido");
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${idPersona.nombre}"));
        columnBinding.setColumnName("Id Persona.nombre");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${idPersona.nroDoc}"));
        columnBinding.setColumnName("Id Persona.nro Doc");
        columnBinding.setColumnClass(Double.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("jTable1.columnModel.title2")); // NOI18N
        jTable1.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("jTable1.columnModel.title1")); // NOI18N
        jTable1.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("jTable1.columnModel.title2")); // NOI18N

        contraer.setText(resourceMap.getString("contraer.text")); // NOI18N
        contraer.setName("contraer"); // NOI18N
        contraer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                contraerActionPerformed(evt);
            }
        });

        jTextField2.setEditable(false);
        jTextField2.setText(resourceMap.getString("jTextField2.text")); // NOI18N
        jTextField2.setName("jTextField2"); // NOI18N

        javax.swing.GroupLayout jPanelBuscarLayout = new javax.swing.GroupLayout(jPanelBuscar);
        jPanelBuscar.setLayout(jPanelBuscarLayout);
        jPanelBuscarLayout.setHorizontalGroup(
            jPanelBuscarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelBuscarLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelBuscarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 768, Short.MAX_VALUE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 768, Short.MAX_VALUE)
                    .addComponent(contraer)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanelBuscarLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buscar)))
                .addContainerGap())
        );
        jPanelBuscarLayout.setVerticalGroup(
            jPanelBuscarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelBuscarLayout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanelBuscarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(buscar, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(60, 60, 60)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(contraer, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanelAtencion.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanelAtencion.setName("jPanelAtencion"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setFont(resourceMap.getFont("jLabel5.font")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setFont(resourceMap.getFont("jLabel5.font")); // NOI18N
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jScrollPane2.setFocusable(false);
        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setRows(5);
        jTextArea1.setAutoscrolls(false);
        jTextArea1.setName("jTextArea1"); // NOI18N
        jScrollPane2.setViewportView(jTextArea1);

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setFont(resourceMap.getFont("jLabel5.font")); // NOI18N
        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jCheckBox1.setText(resourceMap.getString("jCheckBox1.text")); // NOI18N
        jCheckBox1.setEnabled(false);
        jCheckBox1.setName("jCheckBox1"); // NOI18N

        jCheckBox2.setText(resourceMap.getString("jCheckBox2.text")); // NOI18N
        jCheckBox2.setEnabled(false);
        jCheckBox2.setName("jCheckBox2"); // NOI18N

        jCheckBox3.setText(resourceMap.getString("jCheckBox3.text")); // NOI18N
        jCheckBox3.setEnabled(false);
        jCheckBox3.setName("jCheckBox3"); // NOI18N

        jPanel3.setName("jPanel3"); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel8.setIcon(resourceMap.getIcon("jLabel8.icon")); // NOI18N
        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jLabel9.setIcon(resourceMap.getIcon("jLabel9.icon")); // NOI18N
        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        jLabel10.setFont(resourceMap.getFont("jLabel10.font")); // NOI18N
        jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N

        jLabel11.setFont(resourceMap.getFont("jLabel11.font")); // NOI18N
        jLabel11.setText(resourceMap.getString("jLabel11.text")); // NOI18N
        jLabel11.setName("jLabel11"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(79, 79, 79)
                        .addComponent(jLabel10))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(82, 82, 82)
                        .addComponent(jLabel11))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel8))))
                .addContainerGap(56, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel9)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jScrollPane4.setName("jScrollPane4"); // NOI18N

        jTTurnos.setName("jTTurnos"); // NOI18N
        jTTurnos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTTurnos.getTableHeader().setReorderingAllowed(false);

        jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, turnoList, jTTurnos);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${hora}"));
        columnBinding.setColumnName("Hora");
        columnBinding.setColumnClass(Long.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${duracion}"));
        columnBinding.setColumnName("Duracion");
        columnBinding.setColumnClass(Long.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${practicaxempleado.empleado.idPersona.apellido}"));
        columnBinding.setColumnName("Practicaxempleado.empleado.id Persona.apellido");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${practicaxempleado.practica.nombre}"));
        columnBinding.setColumnName("Practicaxempleado.practica.nombre");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jTTurnos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTTurnosMouseClicked(evt);
            }
        });
        jScrollPane4.setViewportView(jTTurnos);
        jTTurnos.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("jTTurnos.columnModel.title0")); // NOI18N
        jTTurnos.getColumnModel().getColumn(0).setCellRenderer(new DateRenderer(1));
        jTTurnos.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("jTTurnos.columnModel.title1")); // NOI18N
        jTTurnos.getColumnModel().getColumn(1).setCellRenderer(new DateRenderer(4));
        jTTurnos.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("jTTurnos.columnModel.title2")); // NOI18N
        jTTurnos.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("jTTurnos.columnModel.title3")); // NOI18N

        jLabel12.setFont(resourceMap.getFont("jLabel12.font")); // NOI18N
        jLabel12.setText(resourceMap.getString("jLabel12.text")); // NOI18N
        jLabel12.setName("jLabel12"); // NOI18N

        javax.swing.GroupLayout jPanelAtencionLayout = new javax.swing.GroupLayout(jPanelAtencion);
        jPanelAtencion.setLayout(jPanelAtencionLayout);
        jPanelAtencionLayout.setHorizontalGroup(
            jPanelAtencionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelAtencionLayout.createSequentialGroup()
                .addGroup(jPanelAtencionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanelAtencionLayout.createSequentialGroup()
                        .addGroup(jPanelAtencionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanelAtencionLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanelAtencionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanelAtencionLayout.createSequentialGroup()
                                        .addGap(25, 25, 25)
                                        .addGroup(jPanelAtencionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel4)
                                            .addComponent(jLabel5))
                                        .addGap(10, 10, 10)
                                        .addGroup(jPanelAtencionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 352, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE))
                                        .addGap(25, 25, 25))
                                    .addGroup(jPanelAtencionLayout.createSequentialGroup()
                                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 356, Short.MAX_VALUE))
                                    .addGroup(jPanelAtencionLayout.createSequentialGroup()
                                        .addComponent(jLabel7)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(jPanelAtencionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jCheckBox2, javax.swing.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE)
                                            .addComponent(jCheckBox1, javax.swing.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE)
                                            .addComponent(jCheckBox3, javax.swing.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE)))))
                            .addGroup(jPanelAtencionLayout.createSequentialGroup()
                                .addGap(101, 101, 101)
                                .addComponent(jLabel12)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 353, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(66, 66, 66)))
                .addContainerGap())
        );
        jPanelAtencionLayout.setVerticalGroup(
            jPanelAtencionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelAtencionLayout.createSequentialGroup()
                .addGroup(jPanelAtencionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelAtencionLayout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanelAtencionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelAtencionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                        .addGroup(jPanelAtencionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(jCheckBox1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCheckBox2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCheckBox3)
                        .addGap(22, 22, 22)
                        .addGroup(jPanelAtencionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(52, 52, 52))
                    .addGroup(jPanelAtencionLayout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        jPanelBotonera.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanelBotonera.setName("jPanelBotonera"); // NOI18N

        jButton1.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setEnabled(false);
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jToggleButton1.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        jToggleButton1.setText(resourceMap.getString("jToggleButton1.text")); // NOI18N
        jToggleButton1.setName("jToggleButton1"); // NOI18N
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        jPanelHc.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanelHc.setName("jPanelHc"); // NOI18N
        jPanelHc.setLayout(null);

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        jTable2.setName("jTable2"); // NOI18N

        jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, turnoList, jTable2);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${hora}"));
        columnBinding.setColumnName("Hora");
        columnBinding.setColumnClass(Long.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${idAtencion.detalleAtencion}"));
        columnBinding.setColumnName("Id Atencion.detalle Atencion");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${idAtencion.evoPaciente}"));
        columnBinding.setColumnName("Id Atencion.evo Paciente");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jTable2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable2MouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(jTable2);
        jTable2.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("jTable2.columnModel.title0")); // NOI18N
        jTable2.getColumnModel().getColumn(0).setCellRenderer(new DateRenderer(1));
        jTable2.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("jTable2.columnModel.title1")); // NOI18N
        jTable2.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("jTable2.columnModel.title2")); // NOI18N

        jPanelHc.add(jScrollPane3);
        jScrollPane3.setBounds(10, 10, 730, 430);

        javax.swing.GroupLayout jPanelBotoneraLayout = new javax.swing.GroupLayout(jPanelBotonera);
        jPanelBotonera.setLayout(jPanelBotoneraLayout);
        jPanelBotoneraLayout.setHorizontalGroup(
            jPanelBotoneraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelBotoneraLayout.createSequentialGroup()
                .addGroup(jPanelBotoneraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelBotoneraLayout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jToggleButton1))
                    .addGroup(jPanelBotoneraLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jPanelHc, javax.swing.GroupLayout.DEFAULT_SIZE, 758, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanelBotoneraLayout.setVerticalGroup(
            jPanelBotoneraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelBotoneraLayout.createSequentialGroup()
                .addGroup(jPanelBotoneraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jToggleButton1))
                .addGap(18, 18, 18)
                .addComponent(jPanelHc, javax.swing.GroupLayout.DEFAULT_SIZE, 470, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanelBotonera, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jPanelBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanelAtencion, 0, 782, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanelBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelAtencion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanelBotonera, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        bindingGroup.bind();

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-800)/2, (screenSize.height-608)/2, 800, 608);
    }// </editor-fold>//GEN-END:initComponents

private void buscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarActionPerformed
    jPanelBuscar.setSize(800, 280);
}//GEN-LAST:event_buscarActionPerformed

private void contraerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_contraerActionPerformed
    jPanelBuscar.setSize(800, 65);

}//GEN-LAST:event_contraerActionPerformed

private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
    if (jPanelAtencion.isVisible()) {
        jPanelAtencion.setVisible(false);
        jToggleButton1.setText("Ocultar Hist Clínica");
        jTable2.updateUI();
        jTable2.repaint();
    } else {
        jPanelAtencion.setVisible(true);
        jToggleButton1.setText("Ver Historia Clínica");
        jTTurnos.updateUI();
        jTTurnos.repaint();
    }

}//GEN-LAST:event_jToggleButton1ActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    NvaAtencion nvaAtencion;
    if (nuevoFlag) {
        nvaAtencion = new NvaAtencion(turnoSelected);
    } else {
        nvaAtencion = new NvaAtencion(pacienteselec, atencion);
    }

    nvaAtencion.setVisible(true);
}//GEN-LAST:event_jButton1ActionPerformed

private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyReleased
    try {
        int doc = Integer.parseInt(jTextField1.getText());
        pacienteQuery = PodologiaPUEntityManager.createNativeQuery("SELECT * FROM Paciente p, Persona pe WHERE p.idPersona=pe.idpersona and pe.nroDoc like '" + jTextField1.getText() + "%'", podologia.beansDB.Paciente.class);
    } catch (NumberFormatException ex) {
        pacienteQuery = PodologiaPUEntityManager.createQuery("SELECT p FROM Paciente p WHERE p.idPersona.nombre like '" + jTextField1.getText() + "%' or p.idPersona.apellido like '" + jTextField1.getText() + "%'");
    }
    pacienteList.clear();
    pacienteList.addAll(pacienteQuery.getResultList());
    jTable1.updateUI();
    jTable1.repaint();

}//GEN-LAST:event_jTextField1KeyReleased

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
    pacienteselec = (Paciente) pacienteList.get(jTable1.getSelectedRow());
    //jTextArea1.setText(pacienteselec.getDetalle());
    jTextField2.setText(((Persona) pacienteselec.getIdPersona()).getApellido() + " " + ((Persona) pacienteselec.getIdPersona()).getNombre());

    //Se busca el listado de Turnos del Paciente Seleccionado en la Tabla
    turnoQuery = PodologiaPUEntityManager.createQuery("SELECT t FROM Turno t WHERE t.idPaciente=:param ORDER BY t.hora desc");
    turnoQuery.setParameter("param", pacienteselec);
    turnoList.clear();
    turnoList.addAll(turnoQuery.getResultList());

    //Trae la ultima atencion, buscando el ultimo turno con atencion asociada
    Turno ultimoTurno = null;
    atencion = null;

    for (Turno turnoAux : turnoList) {
        if (turnoAux.getIdAtencion() != null) {
            if (turnoAux.getIdAtencion().getIdAtencion() > 0) {
                atencion = turnoAux.getIdAtencion();
                ultimoTurno = turnoAux;
                break;
            }
        }
    }


    //Setea los componentes Visuales con los datos de la última atención
    if (atencion != null) {
        jTextArea1.setText(atencion.getDetalleAtencion());
        dia.setTimeInMillis(ultimoTurno.getHora());
        SimpleDateFormat ds = new SimpleDateFormat("dd/MM/yyyy");
        jLabel6.setText(ds.format(dia.getTime()));
        if (atencion.getIdExaNeuro() != null) {
            jCheckBox1.setSelected(true);
        }
        if (atencion.getIdExaParam() != null) {
            jCheckBox2.setSelected(true);
        }
        if (atencion.getIdExaVasc() != null) {
            jCheckBox3.setSelected(true);
        }

        if (atencion.getImagen1() != null) {
            File fAux = new File(atencion.getImagen1());
            if (fAux.exists()) {
                ImageIcon ico = new ImageIcon(atencion.getImagen1());
                Image img = ico.getImage().getScaledInstance(120, 120, 260);
                ico.setImage(img);
                jLabel8.setIcon(ico);
            } else {
                org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(podologia.PodologiaApp.class).getContext().getResourceMap(Atencion.class);
                jLabel8.setIcon(resourceMap.getIcon("jLabel8.icon"));
            }
        } else {
            org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(podologia.PodologiaApp.class).getContext().getResourceMap(Atencion.class);
            jLabel8.setIcon(resourceMap.getIcon("jLabel8.icon"));
        }

        if (atencion.getImagen2() != null) {
            File fAux2 = new File(atencion.getImagen1());
            if (fAux2.exists()) {
                ImageIcon ico = new ImageIcon(atencion.getImagen2());
                Image img = ico.getImage().getScaledInstance(120, 120, 260);
                ico.setImage(img);
                jLabel9.setIcon(ico);
            } else {
                org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(podologia.PodologiaApp.class).getContext().getResourceMap(Atencion.class);
                jLabel9.setIcon(resourceMap.getIcon("jLabel9.icon"));
            }
        } else {
            org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(podologia.PodologiaApp.class).getContext().getResourceMap(Atencion.class);
            jLabel9.setIcon(resourceMap.getIcon("jLabel9.icon"));
        }

    } else {
        jTextArea1.setText("");
        jLabel6.setText("Sin Atenciones");
        jCheckBox1.setSelected(false);
        jCheckBox2.setSelected(false);
        jCheckBox3.setSelected(false);
        jLabel8.setIcon(null);
        jLabel9.setIcon(null);
    }
    if (turnoList.isEmpty()) {
        jButton1.setEnabled(false);
    }

    jTTurnos.updateUI();

    jTTurnos.repaint();

    jPanelBuscar.setSize(800, 65);
}//GEN-LAST:event_jTable1MouseClicked

private void jTTurnosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTTurnosMouseClicked
    int fila = jTTurnos.getSelectedRow();
    Turno tr = turnoList.get(fila);
    turnoSelected =
            tr;
    jButton1.setEnabled(true);
    if (tr.getIdAtencion() != null) {
        nuevoFlag = false;
        jButton1.setText("Modificar Atencion");
    } else {
        nuevoFlag = true;
        jButton1.setText("Nueva Atencion");
    }

    pacienteselec = tr.getIdPaciente();
}//GEN-LAST:event_jTTurnosMouseClicked

private void jTable2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable2MouseClicked
    int fila = jTable2.getSelectedRow();
    Turno tr = turnoList.get(fila);
    turnoSelected =
            tr;
    jButton1.setEnabled(true);
    if (tr.getIdAtencion() != null) {
        nuevoFlag = false;
        jButton1.setText("Modificar Atencion");
    } else {
        nuevoFlag = true;
        jButton1.setText("Nueva Atencion");
    }

    pacienteselec = tr.getIdPaciente();
}//GEN-LAST:event_jTable2MouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager PodologiaPUEntityManager;
    private java.util.List<podologia.beansDB.Atencion> atencionList;
    private javax.persistence.Query atencionQuery;
    private javax.swing.JButton buscar;
    private javax.swing.JButton contraer;
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JCheckBox jCheckBox3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanelAtencion;
    private javax.swing.JPanel jPanelBotonera;
    private javax.swing.JPanel jPanelBuscar;
    private javax.swing.JPanel jPanelHc;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTable jTTurnos;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JToggleButton jToggleButton1;
    private java.util.List<podologia.beansDB.Paciente> pacienteList;
    private javax.persistence.Query pacienteQuery;
    private java.util.List<podologia.beansDB.Persona> personaList;
    private javax.persistence.Query personaQuery;
    private java.util.List<podologia.beansDB.Turno> turnoList;
    private javax.persistence.Query turnoQuery;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
